package com.ayit.car.service.impl;

import com.ayit.car.common.context.BaseContext;
import com.ayit.car.common.result.PageResult;
import com.ayit.car.mapper.VehicleExteriorColorsMapper;
import com.ayit.car.mapper.VehicleInteriorColorsMapper;
import com.ayit.car.pojo.VehicleExteriorColors;
import com.ayit.car.pojo.VehicleInteriorColors;
import com.ayit.car.pojo.dto.VehiclesDTO;
import com.ayit.car.pojo.dto.VehiclesPageQueryDTO;
import com.ayit.car.pojo.vo.VehiclesVO;
import com.ayit.car.service.ModelsService;
import com.ayit.car.service.VehicleExteriorColorsService;
import com.ayit.car.service.VehicleInteriorColorsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ayit.car.pojo.Vehicles;
import com.ayit.car.service.VehiclesService;
import com.ayit.car.mapper.VehiclesMapper;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author 为王下葬
 * @description 针对表【vehicles(具体车辆表)】的数据库操作Service实现
 * @createDate 2024-06-24 18:12:48
 */
@Service
public class VehiclesServiceImpl extends ServiceImpl<VehiclesMapper, Vehicles>
        implements VehiclesService {

    @Autowired
    private VehiclesService vehiclesService;
    @Autowired
    private ModelsService modelsService;
    @Autowired
    private VehiclesMapper vehiclesMapper;
    @Autowired
    private VehicleExteriorColorsService vehicleExteriorColorsService;
    @Autowired
    private VehicleInteriorColorsService vehicleInteriorColorsService;
    @Autowired
    private VehicleExteriorColorsMapper vehicleExteriorColorsMapper;
    @Autowired
    private VehicleInteriorColorsMapper vehicleInteriorColorsMapper;

    /**
     * 添加车辆信息
     *
     * @param vehiclesDTO
     */
    @Transactional
    @Override
    public void add(VehiclesDTO vehiclesDTO) {
        Vehicles vehicles = new Vehicles();
        BeanUtils.copyProperties(vehiclesDTO, vehicles);

        vehicles.setCreateUser(BaseContext.getCurrentId());

        //自动填充车型名称
        vehicles.setModelName(modelsService.getModelName(vehiclesDTO.getModelId()));

        //自动填充车身颜色,只是方便展示
        vehicles.setExteriorColorIds(vehiclesDTO.getExteriorColorIds().toString());
        vehicles.setInteriorColorIds(vehiclesDTO.getInteriorColorIds().toString());
        vehiclesService.save(vehicles);

        Long carId = vehicles.getId();

        //添加车身颜色信息
        addColors(vehiclesDTO.getExteriorColorIds(),vehiclesDTO.getInteriorColorIds(), carId);

    }

    private void addColors(List<Integer> exteriorColorIds, List<Integer> interiorColorIds, Long carId) {
        //添加车身颜色信息
        //拿到车身颜色id列表
//        List<Integer> exteriorColorIds = vehiclesDTO.getExteriorColorIds();
        if (exteriorColorIds.size() > 0){
            ArrayList<VehicleExteriorColors> exteriorColors = new ArrayList<>();
            for (Integer exteriorColorId : exteriorColorIds) {
                //构建一个新的车辆信息
                VehicleExteriorColors vehicleExteriorColors = new VehicleExteriorColors();
                vehicleExteriorColors.setVehicleId(carId);
                vehicleExteriorColors.setColorId(Long.valueOf(exteriorColorId));
                exteriorColors.add(vehicleExteriorColors);
            }
            //批量插入
            vehiclesMapper.addBatchExteriorColors(exteriorColors);
        }

        //添加内饰颜色信息
//        List<Integer> interiorColorIds = vehiclesDTO.getInteriorColorIds();
        if (interiorColorIds.size() > 0) {
            ArrayList<VehicleInteriorColors> interiorColors = new ArrayList<>();
            for (Integer interiorColorId : interiorColorIds) {
                VehicleInteriorColors vehicleInteriorColors = new VehicleInteriorColors();
                vehicleInteriorColors.setVehicleId(carId);
                vehicleInteriorColors.setColorId(Long.valueOf(interiorColorId));
                interiorColors.add(vehicleInteriorColors);
            }
            vehiclesMapper.addBatchInteriorColors(interiorColors);
        }
    }

    /**
     * 删除车辆信息
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        vehiclesService.removeByIds(ids);
        //同时删除颜色信息
        vehicleExteriorColorsService.deleteByCarId(ids);
        vehicleInteriorColorsService.deleteByCarId(ids);
    }

    /**
     * 修改车辆信息
     *
     * @param vehiclesVO
     */
    @Transactional
    @Override
    public void updateVehicles(VehiclesVO vehiclesVO) {
        Vehicles vehicles = new Vehicles();
        BeanUtils.copyProperties(vehiclesVO, vehicles);

        vehicles.setUpdateUser(BaseContext.getCurrentId());
        vehicles.setExteriorColorIds(vehiclesVO.getExteriorColorIds().toString());
        vehicles.setInteriorColorIds(vehiclesVO.getInteriorColorIds().toString());

        vehiclesService.updateById(vehicles);

        Long carId = vehiclesVO.getId();

        //删除颜色信息
        vehicleExteriorColorsMapper.deleteByCarId(carId);
        vehicleInteriorColorsMapper.deleteByCarId(carId);

        //获取新车颜色信息,通过String转List<Integer>插入数据
        String exteriorColorIds = vehiclesVO.getExteriorColorIds();
        List<Integer> ex = Arrays.stream(exteriorColorIds.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        //获取新车颜色信息
        String interiorColorIds = vehiclesVO.getInteriorColorIds();
        List<Integer> in = Arrays.stream(interiorColorIds.split(","))
                .map(Integer::parseInt)
                .collect(Collectors.toList());
        //重新添加颜色信息
        addColors(ex, in, carId);

    }

    @Override
    public PageResult pageQuery(VehiclesPageQueryDTO vehiclesPageQueryDTO) {
        PageHelper.startPage(vehiclesPageQueryDTO.getPage(), vehiclesPageQueryDTO.getPageSize());
        Page<VehiclesVO> page = vehiclesMapper.pageQuery(vehiclesPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    @Override
    public VehiclesVO getCarInfo(Long id) {
        return vehiclesMapper.getCarInfo(id);
    }
}




